/**
 * 忘记密码页面
 * @author ganjia
 * @date 2019/08/16 10:14
 */
define(function () {
    // $(function ($) {

        var self,
            app = win.app,
            doc = document,
            vueData = {
                form: {
                    cellphone:'',
                    vercode:'',
                    smscode:'',
                    smscode_back:'',
                    password:'',
                    repass:''

                },
                type : ''
            },

            callObj
        ;
        var curCount=60;//当前剩余秒数
        var InterValObj; //timer变量，控制时间
        var ForgetEdit = app.Module.extend({
            init: function () {
                self = this;
                self._super({
                    _tag: 'forget'
                }, win);
            },
            _call: function (obj) {


            },
            _ready: function () {
                var vue = new Vue({
                    el: '#LAY-user-login',
                    data: vueData,
                    methods: {
                        getSmsCode:function (obj) {
                            if(vueData.form.cellphone==''){
                                app.showError('手机号不可为空');
                                return
                            } else if(vueData.form.cellphone.length !=11){
                                app.showError('手机号格式不正确');
                                return
                            }else if(vueData.form.vercode ==''){
                                app.showError('图形验证码不可为空');
                                return
                            }else {
                                var success=true;
                                app.ajax({
                                    xurl: '/kaptcha/checkVerifyCode',
                                    xdata: {vercode: vueData.form.vercode},
                                    async: false,
                                    cb: function (result) {
                                        if (result.ok) {
                                            if(!result.data.data.verifyResult){
                                                app.showError("验证码校验失败，请重新输入！")
                                                success=false;
                                            }
                                        }
                                    }
                                });
                                if(!success){
                                    return
                                }
                                app.ajax({
                                    xurl: 'admin/checkCellPhone',
                                    xdata: {cellphone: vueData.form.cellphone},
                                    async: false,
                                    cb: function (result) {
                                        if (result.ok) {
                                            var data = result.data.data;
                                            if(data.num > 1){
                                                app.showError('该手机号绑定账号不唯一');
                                                success=false;
                                            }
                                            if(data.num < 1 || data.num==null){
                                                app.showError('该手机号未绑定账号');
                                                success=false;
                                            }
                                        }
                                    }
                                });
                                if(!success){
                                    return
                                }
                            }
                            app.ajax({
                                xurl: 'admin/getSmsCode',
                                xdata: {cellphone:vueData.form.cellphone},
                                async: false,
                                okmsg: '验证码发送成功',
                                cb: function (result) {
                                    if (result.ok) {
                                        var data = result.data.data;
                                        vueData.form.smscode_back=data.smscode_back;

                                        //设置button效果，开始计时
                                        $("#LAY-user-getsmscode").css("background-color", "LightSkyBlue");
                                        $("#LAY-user-getsmscode").attr("disabled", "true");
                                        $("#LAY-user-getsmscode").html("重新发送(" + curCount + "s)");
                                        InterValObj = window.setInterval(self._settime, 1000); //启动计时器，1秒执行一次

                                    }
                                }
                            });
                        },
                        findPassword:function () {
                            if(vueData.form.smscode == ''){
                                app.showError('请先获取短信验证码');
                            }else if(vueData.form.smscode_back == ''){
                                app.showError('请先输入短信验证码');
                            }else if(vueData.form.smscode_back != vueData.form.smscode){
                                app.showError('短信验证码输入不正确')
                            } else {
                                vueData.type='resetpass'
                            }
                        },
                        saveNewPassword:function () {
                            var success=true;
                            if(vueData.form.password !=vueData.form.repass){
                                app.showError('两次密码不一致');
                                return
                            }else {
                                app.ajax({
                                    xurl: 'admin/saveNewPassword',
                                    xdata: {cellphone:vueData.form.cellphone,password:vueData.form.password},
                                    async: false,
                                    okmsg: '密码重置成功',
                                    cb: function (result) {
                                        if (!result.ok) {
                                            success=false;
                                        }
                                    }
                                });
                            }
                            if(success){
                                app.showMessage('密码重置成功,5秒后自动跳转...');
                                setTimeout(function(){
                                    location.href = '../../login';
                                },5000)

                            }else {
                                app.showError('密码重置失败');
                            }

                        },
                        cancel: function () {
                            layer.close(layer.index);
                        },
                        sure: function () {
                            this.$refs['form'].validate(function (valid) {
                                if (valid) {
                                    self.update();
                                }
                            });
                        }
                    },
                    watch: {},
                    created: function () {
                        // self._setEleIsReady('LAY-user-login');
                    }
                });
            },
            _settime:function (obj) {
                if(curCount == 0){
                    window.clearInterval(InterValObj);//停止计时器
                    $("#LAY-user-getsmscode").removeAttr("disabled");//启用按钮
                    $("#LAY-user-getsmscode").css("background-color", "");
                    $("#LAY-user-getsmscode").html("重发验证码");

                }else {
                    curCount--;
                    $("#LAY-user-getsmscode").html("重新发送(" + curCount + "s)");
                }
            },
            _lazy: function () {
                this._super();
            }
        });


        return new ForgetEdit();
    // });

});
